package sc.test.spring.jdbc;

import java.util.Date;
import java.util.HashMap;
import java.util.Set;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import sc.common.test.TestUtil;
import sc.common.test.cmd.CommandContext;
import sc.common.test.cmd.Commander;

public class JdbcCommander implements Commander {

    public static class JdbcRunnable implements Runnable {

        public JdbcRunnable(JdbcTemplate template, String sql, String name, HashMap<String, String> result) {
            super();
            this.template = template;
            this.sql = sql;
            this.name = name;
            this.result = result;
        }

        JdbcTemplate template;
        String sql;
        String name;
        HashMap<String, String> result;

        @Override
        public void run() {
            long begin = System.currentTimeMillis();
            SqlRowSet rs = template.queryForRowSet(sql);
            long end = System.currentTimeMillis();
            JdbcUtil.printMetaData(rs.getMetaData());
            Date beginTime = new Date(begin);
            Date endTime = new Date(end);
            String res = String.format("consume[%10dms], start[%20s], end[%20s]", (end - begin),
                    TestUtil.toReadable(beginTime), TestUtil.toReadable(endTime));
            TestUtil.sp(res);
            //            result.put(name, res);
        }

        @Override
        public String toString() {
            return name;
        }
    }

    public JdbcCommander(BasicDataSource ds) {
        this.ds = ds;
    }

    BasicDataSource ds;

    static HashMap<String, String> result = new HashMap<String, String>();
    static int i = 1;

    @Override
    public void commandReceived(CommandContext ctxt) {
        String cmd = ctxt.getCommand();
        if ("s".equals(cmd)) {
            TestUtil.sp("[op]show");
            StringBuilder sb = new StringBuilder("[op]show\r\n");
            int count = 0;
            //                    Enumeration<String> ke = result.keys();
            //                    while (ke.hasMoreElements()) {
            //                        String key = ke.nextElement();
            //                        sb.append(String.format("(%5d) key[%10s], %30s\r\n", ++count, key, result.get(key)));
            //                    }

            Set<String> ks = result.keySet();
            for (String key : ks) {
                sb.append(String.format("(%5d) key[%10s], %30s\r\n", ++count, key, result.get(key)));
            }
            TestUtil.sp(sb.toString());
        } else if ("a".equals(cmd)) {
            TestUtil.sp("[op]add");
            JdbcTemplate template = new JdbcTemplate(ds);
            new Thread(new JdbcRunnable(template, "select * from basis.tb_uq_define where 1=0", i++ + "", result)).start();
        } else if ("sds".equals(cmd)) {
            TestUtil.sp("[op]show datasource");
            TestUtil.sp("NumActive[%d], NumIdle[%d]", ds.getNumActive(), ds.getNumIdle());
        }
    }
}
